home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / play / tracker_4_31.lzh / tracker / randomize.c < prev    next >
C/C++ Source or Header  |  1995-03-01  |  2KB  |  92 lines

  1. /* randomize.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: randomize.c,v 4.13 1995/03/01 15:24:51 espie Exp espie $ 
  6.  * $Log: randomize.c,v $
  7.  * Revision 4.13  1995/03/01  15:24:51  espie
  8.  * cleanup.
  9.  *
  10.  * Revision 4.12  1995/02/24  13:47:56  espie
  11.  * Minor change for FreeBSD
  12.  *
  13.  * Revision 4.11  1995/02/21  17:54:32  espie
  14.  * Internal problem: buggy RCS. Fixed logs.
  15.  *
  16.  * Revision 4.6  1995/02/01  20:41:45  espie
  17.  * Added color.
  18.  *
  19.  */
  20.  
  21. /* input: a series of names (as argv[1:argc - 1])
  22.  * output: the same names, in a random order.
  23.  * with the new database lookup facility, very useful for e.g.,
  24.  * tracker `randomize *` (jukebox)
  25.  */
  26.  
  27. #include "defs.h"
  28. #include <sys/types.h>
  29. #ifdef IS_POSIX
  30. #include <time.h>
  31. #else
  32. #include <sys/time.h>
  33. #endif
  34.  
  35. ID("$Id: randomize.c,v 4.13 1995/03/01 15:24:51 espie Exp espie $")
  36.  
  37. /* n = random_range(max): output a number in the range 0:max - 1.
  38.  * For our purpose, we don't have to get a very random number,
  39.  * so the standard generator is alright.
  40.  */
  41. int random_range(max)
  42. int max;
  43.     {
  44.     static init = 0;
  45.  
  46.         /* initialize the generator to an appropriate seed eventually */
  47.     if (!init)
  48.         {
  49.         srand(time(0));
  50.         init = 1;
  51.         }
  52.     return rand()%max;
  53.     }
  54.  
  55. /* output(s): output s in a suitable format. Ideally, output() should use
  56.  * the shell quoting conventions for difficult names. Right now, it doesn't
  57.  */
  58. void output(s)
  59. char *s;
  60.     {
  61.     for(; *s; s++)
  62.         switch(*s)
  63.             {
  64.     /*    case ' ':
  65.         case '(':
  66.         case ')':
  67.         case '\\':
  68.             putchar('\\');
  69.             */
  70.         default:
  71.             putchar(*s);
  72.             }
  73.     putchar(' ');
  74.     }
  75.  
  76. int main(argc, argv)
  77. int argc;
  78. char *argv[];
  79.     {
  80.     int i, k;
  81.  
  82.         /* set up everything so that our names are in argv[0 : argc - 2] */
  83.     for (i = argc - 1, argv++; i; i--)
  84.         {
  85.             /* invariant: the remaining names are in argv[0: i - 1] */
  86.         k = random_range(i);
  87.         output(argv[k]);
  88.         argv[k] = argv[i - 1];
  89.         }
  90.    exit(0);
  91.     }
  92.